package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.ManageService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import sun.nio.cs.ext.MacRoman;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;

    //查询所有分类列表
    @Override
    public PageVO<ProductCategoryTreeNodeVO> categoryTree(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();

        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(productCategoryTreeNodeVOS);
        return new PageVO<>(info.getTotal(),productCategoryTreeNodeVOS);
    }

    //获取分类树节点结构
    private List<ProductCategoryTreeNodeVO> getProductCategoryTreeNodeVOS() {
        Example o1 = new Example(ProductCategory.class);
        Example.Criteria criteria1 = o1.createCriteria();
        o1.setOrderByClause("sort asc");
        criteria1.andEqualTo("pid", 0);

        //一级分类
        return productCategoryMapper.selectByExample(o1).stream().map(productCategory1 -> {
            ProductCategoryTreeNodeVO productCategoryTreeNodeVO1 = new ProductCategoryTreeNodeVO();
            BeanUtils.copyProperties(productCategory1,productCategoryTreeNodeVO1);
            productCategoryTreeNodeVO1.setLev(productCategory1.CATEGIRY_LEV1);

            //二级分类
            Example o2 = new Example(ProductCategory.class);
            Example.Criteria criteria2 = o2.createCriteria();
            o2.setOrderByClause("sort asc");
            criteria2.andEqualTo("pid", productCategory1.getId());
            List<ProductCategoryTreeNodeVO> productCategories2 = productCategoryMapper.selectByExample(o2).stream().map(productCategory2 -> {
                ProductCategoryTreeNodeVO productCategoryTreeNodeVO2 = new ProductCategoryTreeNodeVO();
                BeanUtils.copyProperties(productCategory2,productCategoryTreeNodeVO2);
                productCategoryTreeNodeVO2.setLev(productCategory2.CATEGIRY_LEV2);

                //三级分类
                Example o3 = new Example(ProductCategory.class);
                Example.Criteria criteria3 = o3.createCriteria();
                o3.setOrderByClause("sort asc");
                criteria3.andEqualTo("pid", productCategory2.getId());
                List<ProductCategoryTreeNodeVO> productCategories3 = productCategoryMapper.selectByExample(o3).stream().map(productCategory3 -> {
                    ProductCategoryTreeNodeVO productCategoryTreeNodeVO3 = new ProductCategoryTreeNodeVO();
                    BeanUtils.copyProperties(productCategory3,productCategoryTreeNodeVO3);
                    productCategoryTreeNodeVO3.setLev(productCategory3.CATEGIRY_LEV3);
                    return productCategoryTreeNodeVO3;
                }).collect(Collectors.toList());
                productCategoryTreeNodeVO2.setChildren(productCategories3);
                return productCategoryTreeNodeVO2;
            }).collect(Collectors.toList());
            productCategoryTreeNodeVO1.setChildren(productCategories2);
            return productCategoryTreeNodeVO1;
        }).collect(Collectors.toList());
    }

    //获取父级分类下拉列表
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = getProductCategoryTreeNodeVOS();
        return productCategoryTreeNodeVOS;
    }

    //添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insertSelective(productCategory);
    }

    //编辑分类
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = new ProductCategoryVO();
        BeanUtils.copyProperties(productCategory,productCategoryVO);
        return productCategoryVO;
    }

    //修改分类
    @Override
    public void update(Long id, ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(productCategoryVO,productCategory);
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    //删除分类
    @Override
    public void delete(Long id) {
        Example o1 = new Example(ProductCategory.class);
        Example.Criteria criteria1 = o1.createCriteria();
        criteria1.andEqualTo("pid",id);
        List<ProductCategory> productCategoryList = productCategoryMapper.selectByExample(o1);
        Example o2 = new Example(Product.class);
        Example.Criteria criteria2 = o2.createCriteria();
        criteria2.andEqualTo("threeCategoryId",id);
        Product product = productMapper.selectOneByExample(o2);
        if (CollectionUtils.isEmpty(productCategoryList) && product == null){
            productCategoryMapper.deleteByPrimaryKey(id);
        }
    }

    /*//类别列表展示
    @Override
    public List<ProductCategory> categoryTree(ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        List<ProductCategory> productCategoryList = getProductCategoryTreeNodeVOS().stream().map(productCategoryTreeNodeVO1 -> {
            ProductCategory productCategory = new ProductCategory();
            BeanUtils.copyProperties(productCategoryTreeNodeVO1,productCategory);
            return productCategory;
        }).collect(Collectors.toList());
        return productCategoryList;
    }*/
}
